/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ch.haclyon.driveimageview.driveimageview;

import ch.haclyon.driveimageview.DriveImageModel;
import ch.haclyon.driveimageview.DriveImageView;
import ch.haclyon.driveimageview.driveimageview.slice.MainAbilitySlice;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.List;

/**
 * DisplayItemAdapter
 *
 * @author wangyin
 * @since 2021/07/16
 */
public class DisplayItemAdapter extends BaseItemProvider {
    /**
     * list
     */
    private final List<String> list;

    /**
     * context
     */
    private final Context context;

    /**
     * DisplayItemAdapter
     *
     * @param list list
     * @param context context
     */
    public DisplayItemAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()){
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item, null, false);
        } else {
            cpt = convertComponent;
        }
        String ftext = "photo number " + (position + 1) + " of " + MainAbilitySlice.samplePictures.length;
        DriveImageView view= (DriveImageView) cpt.findComponentById(ResourceTable.Id_driveImageView);
        DriveImageModel model = new DriveImageModel(list.get(position), ftext,
                MainAbilitySlice.samplePictures[position]);
        view.setDriveImageModel(model);
        view.setBackgroundColor(MainAbilitySlice.sampleColours[position]);
        view.setCustomFolderSpacing(100f);
        view.setAlphaValue(0.7f);
        view.setCustomHeight(MainAbilitySlice.sampleHeights[position]);
        return cpt;
    }
}
